import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueDevTools from "vite-plugin-vue-devtools";
import openInEditor from "launch-editor-middleware"; // 引入 openInEditor

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools({
      componentInspector: true,
      launchEditor: "webstorm",
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    proxy: {
      "/api": {
        target: "http://127.0.0.1:8080", // 后端地址
        changeOrigin: true,
        ws: true,
        // rewrite: path => path.replace(/^\/api/, '') // 去掉 /api 前缀
      },
    },
  },
  configureServer(server) {
    // 配置 launch-editor-middleware 来指定 WebStorm 的路径
    server.middlewares.use(
      "/__open-in-editor",
      openInEditor({
        // 使用 WebStorm 的完整路径
        editor: "E:/WebStorm 2024.3.5/bin/webstorm64.exe", // 完整路径
        fallback: "webstorm", // 如果无法通过路径启动，尝试 webstorm 命令
      }),
    );
  },
});
